package com.cloudz.iptv;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.ViewTarget;
import com.cloudz.iptv.activity.BaseActivity;
import com.cloudz.iptv.api.ApiConstants;
import com.cloudz.iptv.bing.BingActivity;
import com.cloudz.iptv.utils.DeviceUuidFactory;
import com.cloudz.iptv.utils.SimplePreference;

import org.apache.commons.lang3.StringUtils;

import butterknife.BindView;

public class SplashActivity extends BaseActivity
{

    public final static String SP_DEVICES_ID = "SP_DEVICES_ID";

    @BindView(R.id.loading_bg)
    public RelativeLayout loading_bg;

    @Override
    public int getLayoutId()
    {
        return R.layout.activity_splash;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Glide.with(this).load("http://pic1.win4000.com/wallpaper/b/57d90dea2a709.jpg").placeholder(R.drawable.placeholder).into(new ViewTarget<View, GlideDrawable>(loading_bg)
        {
            @Override
            public void onResourceReady(GlideDrawable glideDrawable, GlideAnimation<? super GlideDrawable> glideAnimation)
            {
                this.view.setBackground(glideDrawable.getCurrent());
            }
        });

        // 设置请求地址
        SimplePreference.putString(SplashActivity.this, ApiConstants.BASE_URL, "http://tv.yanjisc.cn");

        //延迟2S跳转
        new Handler().postDelayed(new Runnable()
        {
            @Override
            public void run()
            {

                //获取缓存在 sharepreference 里面的 设备唯一标识
                SharedPreferences sharedPreferences = SimplePreference.getPreference(SplashActivity.this);

                String deviceID = sharedPreferences.getString(SP_DEVICES_ID, "");

                if (StringUtils.isBlank(deviceID))
                {
                    // 获取唯一ID
                    String readDeviceID = new DeviceUuidFactory(SplashActivity.this).getUuid().toString();
                    SimplePreference.putString(SplashActivity.this, SP_DEVICES_ID, readDeviceID);
                }

                //验证是否绑定
                String token = sharedPreferences.getString(ApiConstants.BING_TOKEN, "");

                Class<?> cls;

                if (StringUtils.isBlank(token))
                {
                    cls = BingActivity.class;

                } else
                {
                    ApiConstants.IPTV_TOKEN = token;
                    cls = MainActivity.class;
                    // validateBing(readDeviceID);
                }

                Intent intent = new Intent(SplashActivity.this, cls);

                startActivity(intent);

                finish();

            }
        }, 3000);

    }

}

